package cn.itsource.basic.service.impl;

import cn.itsource.basic.basicmapper.BasicMapper;
import cn.itsource.basic.domain.BaseDomain;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.util.PageList;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description:
 * @Author: ZHl
 * @Date: 2022-09-14 16:35
 */
public class BaseServiceImpl<T extends BaseDomain,Q extends BaseQuery> implements IBaseService<T,Q> {


    @Autowired
    private BasicMapper<T> basicMapper;

    @Override
    public T findOne(Long id) {
        return basicMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<T> findAll() {
        return basicMapper.selectAll();
    }

    @Override
    public void save(T department) {
        basicMapper.insertSelective(department); //先新增-->拿到主键id用于拼接dirPath

        this.update(department);
    }

    @Override
    public void update(T department) {
        basicMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public void delete(Long id) {
        basicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageList<T> pageList(Q q) {
        PageHelper.startPage(q.getCurrentPage(), q.getPageSize());

        List<T> ts = basicMapper.selectAll();

        //拿到分页数据
        PageInfo<T> pageInfo = new PageInfo<>(ts);
        //封装分页返回给前端的数据（total,list）
        PageList<T> pageList = new PageList<>(pageInfo.getTotal(),pageInfo.getList());

        return pageList;
    }


    @Override
    public void deleteBatch(Q q) {
        basicMapper.deleteBatch(q.getIds());
    }


}
